package com.gw.vip.center.service;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;
import com.gw.vip.center.constant.VipCenterConstants;
import com.gw.vip.center.util.RedisUtil;
import com.gw.vip.common.exception.ExceptionEnums;
import com.gw.vip.common.exception.ServiceException;
import io.netty.util.CharsetUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author GW
 * @description
 * @since 2022-04-20
 */
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class PassWordService {
    private final RedisUtil redisUtil;

    /**
     * 密码解密
     * @param encPassWord
     * @param channelId
     * @param unionId
     * @return
     */
    public String decrypt(String encPassWord, String channelId, String unionId){
        String sessionKey = redisUtil.get(VipCenterConstants.SESSION_KEY+":"+channelId+":"+unionId);
        if (StringUtils.isBlank(sessionKey)){
            log.error("需要先获取sessionKey");
            throw new ServiceException(ExceptionEnums.BUSINESS_FORBIDDEN, "需要先获取sessionKey");
        }
        byte[] key = sessionKey.getBytes();
        AES aes = SecureUtil.aes(key);
        return aes.decryptStr(encPassWord, CharsetUtil.UTF_8);
    }
}
